/*
 * Copyright 2011 Michael Guo
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.uc.cs.guome.ir;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * The PostingsList object
 * @author(name="Michael Guo",date="01/18/2011")
 * @version 0.1
 */
public class PostingsList implements Serializable{
	
	private static final long serialVersionUID = -1456290463240821901L;
	private short nodeOrder;
	private LinkedList<PostingsListNode> postingsList;
	
	public static final short INSERT_ORDER = 0;
	public static final short TF_DESCENDING_ORDER = 1;
	public static final short TF_ASCENDING_ORDER = 2;
	public static final short ID_DESCENDING_ORDER = 3;
	public static final short ID_ASCENDING_ORDER = 4;
	
	public PostingsList() {
		setPostingsList(new LinkedList<PostingsListNode>());
		setNodeOrder(INSERT_ORDER);
	}
	
	public PostingsList(short order) {
		setPostingsList(new LinkedList<PostingsListNode>());
		setNodeOrder(order);
	}

	public void setPostingsList(LinkedList<PostingsListNode> postingsList) {
		this.postingsList = postingsList;
	}

	public LinkedList<PostingsListNode> getPostingsList() {
		return postingsList;
	}
	
	/**
	 * add a node to the list
	 * @param id
	 * @param tf
	 */
	public void addNode(int id, TermFrequency tf) {
		if (this.nodeOrder == TF_DESCENDING_ORDER){
			//TODO
		}else { //INSERT_ORDER
			this.postingsList.add(new PostingsListNode(id, tf));
		}
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		Iterator<PostingsListNode> i = this.postingsList.iterator();
		while (i.hasNext()){
			sb.append(i.next().toString() + "-");
		}
		sb.deleteCharAt(sb.lastIndexOf("-"));
		return sb.toString();
	}

	public void setNodeOrder(short nodeOrder) {
		this.nodeOrder = nodeOrder;
	}

	public short getNodeOrder() {
		return nodeOrder;
	}
}
